package com.etang.a2020b1006.fragment;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.bizideal.smarthome.socket.LoginCallback;
import com.bizideal.smarthome.socket.SocketClient;
import com.etang.a2020b1006.R;
import com.etang.a2020b1006.tools.DiyToast;

import java.util.ArrayList;
import java.util.List;

public class BarActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    List<Fragment> faFragments = new ArrayList<Fragment>();
    ImageView iv_web_state;
    TextView tv_name;
    RadioButton ra_value, ra_index, ra_link;
    int number = 0;
    boolean is_web = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar);
        iv_web_state = (ImageView) findViewById(R.id.iv_web_state);
        tv_name = (TextView) findViewById(R.id.tv_pager_name);
        ra_index = (RadioButton) findViewById(R.id.ra_index);
        ra_link = (RadioButton) findViewById(R.id.ra_link);
        ra_value = (RadioButton) findViewById(R.id.ra_values);
        faFragments.add(new ValuesFragment());
        faFragments.add(new BaseFragment());
        faFragments.add(new LinkFragment());
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setCurrentItem(1);
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        Log.e("TAGA", "onPageSelected: 123123123123");
                        switch (position) {
                            case 1:
                                tv_name.setText("家庭首页");
                                ra_index.setChecked(true);
                                break;
                            case 0:
                                tv_name.setText("阈值设置");
                                ra_value.setChecked(true);
                                break;
                            case 2:
                                tv_name.setText("联动模式");
                                ra_link.setChecked(true);
                                break;

                        }
                    }
                });
        mViewPager.setOffscreenPageLimit(faFragments.size());
        ControlUtils.setUser("bizideal", "123456", SocketClient.ip);
        SocketClient.getInstance().creatConnect();
        SocketClient.getInstance().login(new LoginCallback() {

            @Override
            public void onEvent(final String arg0) {
                // TODO Auto-generated method stub
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        if (arg0.equals(ConstantUtil.SUCCESS)) {
                            DiyToast.showToast(getApplicationContext(),
                                    "网络连接成功");
                            is_web = false;
                            iv_web_state.setBackgroundResource(R.drawable.web_online);
                        } else if (arg0.equals(ConstantUtil.FAILURE)) {
                            DiyToast.showToast(getApplicationContext(),
                                    "网络连接失败，正在尝试重新连接");
                            is_web = true;
                            iv_web_state.setBackgroundResource(R.drawable.web_error);
                        } else {
                            DiyToast.showToast(getApplicationContext(),
                                    "请稍后，连接中");
                            is_web = true;
                            iv_web_state.setBackgroundResource(R.drawable.web_online);
                        }
                    }
                });
            }
        });
        ra_value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(0);
            }
        });
        ra_index.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(1);
            }
        });
        ra_link.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(2);
            }
        });
        handler.post(timeRunna);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (is_web) {
                if (number % 2 == 0) {
                    iv_web_state.setBackgroundResource(R.drawable.web_online);
                } else {
                    iv_web_state.setBackgroundResource(R.drawable.web_error);
                }
            }
            handler.postDelayed(timeRunna, 500);
        }
    };
    Runnable timeRunna = new Runnable() {
        @Override
        public void run() {
            number++;
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    @Override
    public void onTabSelected(ActionBar.Tab tab,
                              FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return faFragments.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return faFragments.get(arg0);
        }
    }
}
